package com.jit.monitor.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.View;

import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.baselib.util.ui.StatusBarHelper;
import com.jit.monitor.R;
import com.jit.monitor.model.cache.UserCache;
import com.jit.monitor.ui.activity.user.LoginActivity;
import com.jit.monitor.widget.timer.BaseTimeTask;
import com.jit.monitor.widget.timer.BaseTimeTaskListener;

import java.util.Timer;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;


/**
 * @author crazyZhangxl on 2018-11-16 8:15:09.
 * Describe: 引导页
 */

@RuntimePermissions
public class SplashActivity extends BaseActivity implements BaseTimeTaskListener {
    private Timer mTimer;
    private AppCompatTextView mTvTime;
    private int mCount = 4;
    @Override
    protected void init() {

    }

    private void  initTimer(){
        mTimer = new Timer();
        mTimer.schedule(new BaseTimeTask(this),0,1000);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTvTime = findViewById(R.id.tvTime);
        mTvTime.setVisibility(View.GONE);
        SplashActivityPermissionsDispatcher.onNeedsWithCheck(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mTvTime.setOnClickListener(v -> {
            if (mTimer != null){
                mTimer.cancel();
                mTimer = null;
                doJump();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }



    @Override
    public void onBackPressedSupport() {
        // 如果定时器还存在 那么直接返回
        if (mTimer != null){
            return;
        }
        super.onBackPressedSupport();
    }

    private void  doJump(){
        finish();
        // 判断是否登陆
        if (TextUtils.isEmpty(UserCache.getUserToken())){
            jumpToActivity(LoginActivity.class);
        }else{
            MainActivity.show(this);
        }
    }

    @Override
    public void doTimeTask() {
        runOnUiThread(() -> {
           if (mCount > 0){
               mTvTime.setText(String.format("跳过\n%d秒",mCount));
               mCount --;
           }else {
               mTimer.cancel();
               mTimer = null;
               doJump();
           }
        });
    }

    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onNeeds() {
        mTvTime.setVisibility(View.VISIBLE);
        // 开始执行定时的入口哦
        initTimer();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        SplashActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onShow(final PermissionRequest request) {
        showPSMaterialDialog(null, getString(R.string.needs_your_award,getPermissionTips()),
                getString(R.string.ensure), getString(R.string.cancel),
                (dialog, which) -> {
                    dialog.dismiss();
                    request.proceed();
                }, (dialog, which) -> {
                    dialog.dismiss();
                    request.cancel();
                });
    }

    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onPermission() {
        showPSMaterialDialog(getString(R.string.permission_tip_title),getString(R.string.open_needs_pm,
                getPermissionTips()),
                getString(R.string.set_permission_txt), "", (dialog, which) -> {
                    dialog.dismiss();
                    SplashActivityPermissionsDispatcher.onNeedsWithCheck(SplashActivity.this);
                },null);
    }

    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onNeverAsk() {
        showPSMaterialDialog(null,getString(R.string.needs_pm_to_setting,getPermissionTips()), getString(R.string.turn_to_open), "", (dialog, which) -> {
            dialog.dismiss();
            getAppDetailSettingIntent();
        },null);
    }

    private String getPermissionTips(){
        StringBuilder mStringBuilder = new StringBuilder();
        if (lackPermission(Manifest.permission.READ_EXTERNAL_STORAGE) || lackPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            if (TextUtils.isEmpty(mStringBuilder.toString())) {
                mStringBuilder.append(getString(R.string.pm_read_no_space));
            }else {
                mStringBuilder.append(getString(R.string.pm_read_space));
            }
        }

        return mStringBuilder.toString();
    }
}
